package net.muchine.core.support.util;

import java.lang.reflect.Field;
import java.util.List;

import net.muchine.core.domain.user.User;

public class Debugger {
	
	public static <T> String entityToString(T entity) {
		if (entity == null) throw new IllegalArgumentException("Entity parameter is null.");
		
		StringBuffer buffer = new StringBuffer();
		String objectName = entity.getClass().getSimpleName();
		
		buffer.append("print the " + objectName + " entity...\n");
		
		Field[] fields = entity.getClass().getDeclaredFields();
		
		for (Field field : fields) {
			try {
				field.setAccessible(true);
				buffer.append(objectName + "." + field.getName() + ": " + field.get(entity) + "\n");
			} 
			catch (IllegalArgumentException e) {
				e.printStackTrace();
			} 
			catch (IllegalAccessException e) {
				e.printStackTrace();
			}
		}	
				
		return buffer.toString();
	}
	
	public static <T> String listToString(List<T> entities) {
		StringBuffer buffer = new StringBuffer();
		buffer.append("print the list of entities...\n");
		
		int count = 0;
		for (T entity : entities) {
			buffer.append("-- entity[" + count++ + "] --\n");
			buffer.append(entityToString(entity));
		}
		
		return buffer.toString();
	}
	
	public static void main(String[] args) {
		User user = new User();
		user.setId("abc");
		
		System.out.println(Debugger.entityToString(user));
	}
}
